package com.ita.tba.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.tba.po.Cart;
import com.ita.tba.po.Dish;
import com.ita.tba.po.DishCache;
import com.ita.tba.po.User;
import com.ita.tba.service.Controller;
import com.ita.tba.service.impl.ControllerImpl;

/**
 * Servlet implementation class RegistServlet
 */
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Controller controller = new ControllerImpl();
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String email = request.getParameter("email");
//		String password = request.getParameter("password");
//		//PrintWriter writer = response.getWriter();
//		HttpSession session = request.getSession();
//	
//		boolean isEmailExist = controller.isEmailExist(email);
//		if (isEmailExist) {
//			session.setAttribute("error", "Email exist!");
//			request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
//		}
//		else {
//			User user = new User(email, password);
//			controller.addUser(user);
//			session.setAttribute("email", email);
//			
//			Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
//			dishCache = DishCache.dishCache;
//			List<Dish> dishes = new ArrayList<Dish>();
//			for (Integer did : dishCache.keySet()) {
//				dishes.add(dishCache.get(did));
//			}
//			if (dishCache != null) {
//				request.setAttribute("dishes", dishes);
//			}
//			
//			int uid = controller.getUid(email);
//			Cart cart = controller.getThisCart(uid);
//			Map<Integer, Integer> didNum = cart.getDidNum();
//			Map<String, Integer> dnameNum = new HashMap<String, Integer>();
//			for (Integer did : didNum.keySet()) {
//				dnameNum.put(dishCache.get(did).getDname(), didNum.get(did));
//			}
//			double totalPrice = cart.getTotalPrice();
//			dnameNum.put("TotalPrice RMB:", (int) totalPrice);
//			request.setAttribute("cart", dnameNum);
//			
//			request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
//		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		HttpSession session = request.getSession();
	
		boolean isEmailExist = controller.isEmailExist(email);
		if (isEmailExist) {
			session.setAttribute("error", "Email exist!");
			request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
		}
		else {
			User user = new User(email, password);
			controller.addUser(user);
			session.setAttribute("email", email);
			
			Map<Integer, Dish> dishCache = new HashMap<Integer, Dish>();
			dishCache = DishCache.dishCache;
			List<Dish> dishes = new ArrayList<Dish>();
			for (Integer did : dishCache.keySet()) {
				dishes.add(dishCache.get(did));
			}
			if (dishCache != null) {
				request.setAttribute("dishes", dishes);
			}
			
			int uid = controller.getUid(email);
			Cart cart = controller.getThisCart(uid);
			Map<Integer, Integer> didNum = cart.getDidNum();
			Map<String, Integer> dnameNum = new HashMap<String, Integer>();
			for (Integer did : didNum.keySet()) {
				dnameNum.put(dishCache.get(did).getDname(), didNum.get(did));
			}
			double totalPrice = cart.getTotalPrice();
			dnameNum.put("TotalPrice RMB:", (int) totalPrice);
			request.setAttribute("cart", dnameNum);
			
			request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
		}
		
	}

}
